async function initMongodb() {
  var MongoClient = require("mongodb").MongoClient;
  const initData = require("../init/initData.js"); //引入初始化数据
  const config = require("../config.js");
  const client = new MongoClient(config.url);
  try {
    await client.connect();
    const dbase = client.db(config.dbName);
    console.log("数据库连接成功");

    for (const [collectionName, documents] of Object.entries(initData)) {
      const collection = dbase.collection(collectionName);
      const exists = (await collection.countDocuments()) > 0;

      if (exists) {
        console.log(`集合 ${collectionName} 已存在`);
        continue;
      }

      await collection.insertMany(documents);
      console.log(`集合 ${collectionName} 初始化完成`);
    }
  } finally {
    await client.close();
  }
}
module.exports = initMongodb;
